/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.controller;

import java.awt.Dimension;
import java.awt.Point;
import java.util.Locale;
import java.util.prefs.Preferences;

/**
 * The Controller to save and restore global preferences of application
 * Different with data
 * 
 * @author Can
 */
public class PreferenceController {

    private static Preferences preference;

    private PreferenceController() {
    }

    /**
     * do some initialization
     */
    public static int loadPreference() {
        preference = Preferences.userRoot().node("Group3.viCal.preference");
        return 0;
    }

    public static Locale getLocale() {
        String language = preference.get("language", "en");
        return new Locale(language);
    }

    public static void setLocale(final Locale newLocale) {
        preference.put("language", newLocale.getLanguage());
    }

    public static Dimension getSize(final String className, final Dimension defaultSize) {
        int width = preference.getInt(className + ".width", defaultSize.width);
        int height = preference.getInt(className + ".height", defaultSize.height);
        return new Dimension(width, height);
    }

    public static Dimension getSize(final String className) {
        int width = preference.getInt(className + ".width", 800);
        int height = preference.getInt(className + ".height", 500);
        return new Dimension(width, height);
    }

    public static void setSize(final String className, final Dimension size) {
        preference.putInt(className + ".width", size.width);
        preference.putInt(className + ".height", size.height);
    }

    public static Point getLocation(final String className) {
        int x = preference.getInt(className + ".x", 100);
        int y = preference.getInt(className + ".y", 100);
        return new Point(x, y);
    }

    public static Point getLocation(final String className, final Point defaultLocation) {
        int x = preference.getInt(className + ".x", defaultLocation.x);
        int y = preference.getInt(className + ".y", defaultLocation.y);
        return new Point(x, y);
    }

    public static void setLocation(final String className, final Point location) {
        preference.putInt(className + ".x", location.x);
        preference.putInt(className + ".y", location.y);
    }

    public static int getExtendedState(final String className, final int defaultState) {
        return preference.getInt(className + ".extendedState", defaultState);
    }

    public static void setExtendedState(final String className, final int extendedState) {
        preference.putInt(className + ".extendedState", extendedState);
    }
}
